package proyectodbtienda;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;




/**
 *
 * @author  shaka
 */
public class AddData extends javax.swing.JFrame{
    private Connection conn;
    private Statement stmt;
    private Connection connr;
   private Statement stmtr;
    private int flag;

public AddData(){}
public AddData(int flaggg) {
    flag=flaggg;
    System.out.println(flag+"     "+flaggg);
    try {
            initComponents();
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2005 version 1433
            conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1051;databaseName=test;user=sa;password=ecliptica;");//iti
            //conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1055;databaseName=test;user=sa;password=fernanda;");//fer
            stmt = conn.createStatement();
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 2005 version 1433
            connr = DriverManager.getConnection("jdbc:sqlserver://tecnoserver;databaseName=002;user=sa;password=Abc1234.;");//iti
            //conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1055;databaseName=test;user=sa;password=fernanda;");//fer
            stmtr = connr.createStatement();
        } catch ( SQLException excepcionSql){ //excepcionSql = puede ponerle otro nombre
            JOptionPane.showMessageDialog( null, excepcionSql.getMessage(),
            "Error en base de datos", JOptionPane.ERROR_MESSAGE );
            }
            //errores con la carga del controlador de la base de datos(Driver)
            catch ( ClassNotFoundException claseNoEncontrada ){
            JOptionPane.showMessageDialog( null, claseNoEncontrada.getMessage(),
            "No se encontró el controlador", JOptionPane.ERROR_MESSAGE );
            }
    

    }
    /** Creates new form ManejoTablas */
   

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        jlabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();        

        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setOpaque(false);
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

        jButton1.setText("Aceptar");
        jlabel1.setText("etiqueta");
        jPanel3.add(jlabel1);
        jPanel3.add(jButton1);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.add(jPanel3);
       

switch(flag)
{
       case 0:
        jlabel1.setText("Agregar Ciudad");
        jTextField1.setText("Nombre");
        jTextField2.setText("idEstado");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        break;
         case 1:
        jlabel1.setText("Agregar Clientes");
        jTextField1.setText("Nombre");
        jTextField2.setText("    RFC    ");
        jTextField3.setText("telefono");
        jTextField4.setText("direccion");
        jTextField5.setText("idciudad");
        jTextField6.setText("activo");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        jPanel4.add(jTextField4);
        jPanel4.add(jTextField5);
        jPanel4.add(jTextField6);
        break;
    case 2:
        jlabel1.setText("Agregar Compras");
        jTextField1.setText("importe");
        jTextField2.setText("idproveedor");
        jTextField3.setText("fechacompra");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        break;
    case 3:
        jlabel1.setText("Agregar Detalle de Compras");
        jTextField1.setText("IDCompra");
        jTextField2.setText("IDProducto");
        jTextField3.setText("cantidad");
        jTextField4.setText("importe");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        jPanel4.add(jTextField4);
        break;
        case 4:
        jlabel1.setText("Agregar Detalle de Ventas");
        jTextField1.setText("IDVenta");
        jTextField2.setText("IDProducto");
        jTextField3.setText("cantidad");
        jTextField4.setText("importe");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        jPanel4.add(jTextField4);
        break;
    case 5:
        jlabel1.setText("Agregar Estado");
        jTextField1.setText("Nombre Estado");
        jPanel4.add(jTextField1);
        break;
    case 6:
        jlabel1.setText("Agregar Producto");
        jTextField1.setText("nombre");
        jTextField2.setText("precio");
        jTextField3.setText("existencia");
        jTextField4.setText("idproveedor");
        jTextField5.setText("aqctivo");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        jPanel4.add(jTextField4);
        jPanel4.add(jTextField5);
        break;
        case 7:
        jlabel1.setText("Agregar Proveedor");
        jTextField1.setText("    RFC     ");
        jTextField2.setText("nombre");
        jTextField3.setText("dirección");
        jTextField4.setText("aqctivo");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        jPanel4.add(jTextField4);
        break;
        case 8:
        jlabel1.setText("Agregar Ventas");
        jTextField1.setText("idcliente");
        jTextField2.setText("fechaventa");
        jTextField3.setText("total");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        break;
    case 9:
        jlabel1.setText("Agregar Clientes Remotos");
        jTextField1.setText("Nombre");
        jTextField2.setText("    RFC    ");
        jTextField3.setText("telefono");
        jTextField4.setText("direccion");
        jTextField5.setText("idciudad");
        jTextField6.setText("activo");
        jPanel4.add(jTextField1);
        jPanel4.add(jTextField2);
        jPanel4.add(jTextField3);
        jPanel4.add(jTextField4);
        jPanel4.add(jTextField5);
        jPanel4.add(jTextField6);
        break;

         
}
        

        jPanel1.add(jPanel4);

        getContentPane().add(jPanel1);

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        try{
        switch(flag)
            {

        case 0:
                    String ciudad = "\'"+jTextField1.getText()+"\'";
                    int idedo = Integer.parseInt(jTextField2.getText());
                    stmt.executeUpdate("insert into Ciudades values ("+ciudad+","+idedo+")");
                    JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                    this.setVisible(false);
                   
            break;
            case 1:
                String nombre = "\'"+jTextField1.getText()+"\'";
                String rfc = "\'"+jTextField2.getText()+"\'";
                String tel = "\'"+jTextField3.getText()+"\'";
                String dir  = "\'"+jTextField4.getText()+"\'";
                int idciudad = Integer.parseInt(jTextField5.getText());
                String activo = "\'"+jTextField6.getText()+"\'";
                
                stmt.executeUpdate("insert into clientes 9values ("+nombre+","+rfc+
                        ","+tel+","+dir+","+idciudad+","+activo+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                
        break;
            case 2:
                Double importe = Double.parseDouble(jTextField1.getText());
                int idproveedor = Integer.parseInt(jTextField2.getText());
                String fechacompra = "\'"+jTextField3.getText()+"\'";
                stmt.executeUpdate("insert into compras values ("+importe+","+idproveedor+
                        ","+fechacompra+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 3:
                int idcompra = Integer.parseInt(jTextField1.getText());
                int idproducto = Integer.parseInt(jTextField2.getText());
                int cantidad = Integer.parseInt(jTextField3.getText());
                importe  = Double.parseDouble(jTextField4.getText());
                stmt.executeUpdate("insert into detallecompras values ("+idcompra+","+idproducto+
                        ","+cantidad+","+importe+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 4:
                int idventa = Integer.parseInt(jTextField1.getText());
                idproducto = Integer.parseInt(jTextField2.getText());
                cantidad = Integer.parseInt(jTextField3.getText());
                importe  = Double.parseDouble(jTextField4.getText());
                stmt.executeUpdate("insert into detalleVenta values ("+idventa+","+idproducto+
                        ","+cantidad+","+importe+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 5:
                String nombreedo = "\'"+jTextField1.getText()+"\'";
                stmt.executeUpdate("insert into Estados values ("+nombreedo+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 6:
                nombre = "\'"+jTextField1.getText()+"\'";
                double precio = Double.parseDouble(jTextField2.getText());
                int existencia = Integer.parseInt(jTextField3.getText());
                idproveedor  = Integer.parseInt(jTextField4.getText());
                activo = "\'"+jTextField5.getText()+"\'";
                stmt.executeUpdate("insert into productos values ("+nombre+","+precio+
                        ","+existencia+","+idproveedor+","+activo+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 7:
                rfc = "\'"+jTextField1.getText()+"\'";
                nombre = "\'"+jTextField2.getText()+"\'";
                dir = "\'"+jTextField3.getText()+"\'";
                activo = "\'"+jTextField4.getText()+"\'";
                stmt.executeUpdate("insert into proveedores values ("+rfc+","+nombre+
                        ","+dir+","+activo+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 8:                
                int idcliente = Integer.parseInt(jTextField1.getText());
                String fechaventa = "\'"+jTextField2.getText()+"\'";
                double total = Double.parseDouble(jTextField3.getText());
                stmt.executeUpdate("insert into ventas values ("+idcliente+
                        ","+fechaventa+","+total+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            case 9:
                nombre = "\'"+jTextField1.getText()+"\'";
                dir  = "\'"+jTextField4.getText()+"\'";
                tel = "\'"+jTextField3.getText()+"\'";
                idciudad = Integer.parseInt(jTextField5.getText());
                rfc = "\'"+jTextField2.getText()+"\'";
                int activor = Integer.parseInt(jTextField6.getText());
                stmtr.executeUpdate("insert into clientes values ("+nombre+","+dir+
                        ","+tel+","+idciudad+","+rfc+","+activor+")");
                JOptionPane.showMessageDialog(null, "Datos almacenados correctamente");
                this.setVisible(false);
                break;
            }
        } catch (SQLException ex) {
            Logger.getLogger(AddData.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jlabel1;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration

}